<?php
class cls_serve_permission extends cls_serve_base
{
	function has_permission()
	{
		$argvs = func_get_args();
		foreach ($argvs as $permission) {
			if (in_array($permission, $_SESSION['admin']['permission'])) {
				return true;
			}
		}
		return false;
	}
	
	function check_permission()
	{
		$argvs = func_get_args();
		foreach ($argvs as $permission) {
			if (in_array($permission, $_SESSION['admin']['permission'])) {
				return;
			}
		}
		die("你没有相关权限");
	}
	
	function get_mypermission_all($permission)
	{
		$permission_list = $this->get_permission_list();
		$mypermission = explode(',', $permission);
		do {
			$change = false;
			foreach ($permission_list as $permission)
			{
				if ($permission['parent_id'] !== null && !in_array($permission['permission_name'], $mypermission) && in_array($permission_list[$permission['parent_id']]['permission_name'], $mypermission)) {
					$change = true;
					$mypermission[] = $permission['permission_name'];
				}
			}
		}while ($change);
		return $mypermission;
	}
	
	function get_permission_tree()
	{
		$permission_list = $this->get_permission_list();
		$permission_tree = build_tree($permission_list);
		foreach ($permission_tree as $k => $item) {
			$item['user_list'] = explode(',', $item['user_list']);
			$permission_tree[$k] = $this->_merge_user_list($item);
		}
		return $permission_tree;
	}
	
	function get_permission_tree_by_user_name($user_name)
	{
		$permission_list = $this->get_permission_list();
		$user_data = parent::get_data('user');
		$user_info = $user_data->get_user_info($user_name);
		$have_permission = explode(',', $user_info['permission']);
		
		foreach ($permission_list as $k => $permission) {
			if (in_array($permission['permission_name'], $have_permission)) {
				$permission_list[$k]['have'] = true;
			}
		}
		
		$permission_tree = build_tree($permission_list);
		foreach ($permission_tree as $k => $item) {
			$item['user_list'] = explode(',', $item['user_list']);
			$permission_tree[$k] = $this->_merge_user_list($item);
		}
		return $permission_tree;
	}
	
	private function _merge_user_list($permission)
	{
		foreach ($permission['child'] as $k => $item) {
			if (!empty($item['user_list'])) {
				$item['user_list'] = explode(',', $item['user_list']);
			} else {
				$item['user_list'] = array();
			}
			$permission['child'][$k]['user_list'] = array_unique(array_merge($item['user_list'], $permission['user_list']));
			$permission['child'][$k] = $this->_merge_user_list($permission['child'][$k]);
		}
		sort($permission['user_list']);
		$permission['user_list'] = implode(',', $permission['user_list']);
		return $permission;
	}
	
	function get_permission_list()
	{
		$data = parent::get_data();
		$permission_list = $data->get_permission_list();
		return $permission_list;
	}
	
	function update_permission_list($user_name, $update_list)
	{
		$permission_list = $this->get_permission_list();
		$user_data = parent::get_data('user');
		$user_info = $user_data->get_user_info($user_name);
		$have_permission = explode(',', $user_info['permission']);
		$del_list = array();
		$add_list = array();
		
		foreach ($permission_list as $k => $permission) {
			if (in_array($permission['permission_name'], $have_permission) && !in_array($permission['permission_name'], $update_list)) {
				$del_list[] = $permission['permission_name'];
			} else if (!in_array($permission['permission_name'], $have_permission) && in_array($permission['permission_name'], $update_list)) {
				$add_list[] = $permission['permission_name'];
			}
		}
		$data = parent::get_data();
		$data->alloc_permission($user_name, $del_list, 0);
		$data->alloc_permission($user_name, $add_list, 1);
	}
}